// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.


suite("test_cast_to_tinyint_from_decimal64_10_9") {

    // This test case is generated from the correspoinding be UT test case,
    // update this case if the correspoinding be UT test case is updated,
    // e.g.: ../run-be-ut.sh --run --filter=FunctionCastToDecimalTest.* --gen_regression_case
    sql "drop table if exists test_cast_to_tinyint_from_decimal64_10_9_0_nullable;"
    sql "create table test_cast_to_tinyint_from_decimal64_10_9_0_nullable(f1 int, f2 decimalv3(10, 9)) properties('replication_num'='1');"
    sql """insert into test_cast_to_tinyint_from_decimal64_10_9_0_nullable values (0, "0.000000000"),(1, "0.000000000"),(2, "0.000000001"),(3, "-0.000000001"),(4, "0.000000009"),(5, "-0.000000009"),(6, "0.999999999"),(7, "-0.999999999"),(8, "0.999999998"),(9, "-0.999999998"),(10, "0.099999999"),(11, "-0.099999999"),(12, "0.900000000"),(13, "-0.900000000"),(14, "0.900000001"),(15, "-0.900000001"),(16, "1.000000000"),(17, "-1.000000000"),(18, "1.000000001"),(19, "-1.000000001"),
      (20, "1.000000009"),(21, "-1.000000009"),(22, "1.999999999"),(23, "-1.999999999"),(24, "1.999999998"),(25, "-1.999999998"),(26, "1.099999999"),(27, "-1.099999999"),(28, "1.900000000"),(29, "-1.900000000"),(30, "1.900000001"),(31, "-1.900000001"),(32, "9.000000000"),(33, "-9.000000000"),(34, "9.000000001"),(35, "-9.000000001"),(36, "9.000000009"),(37, "-9.000000009"),(38, "9.999999999"),(39, "-9.999999999"),
      (40, "9.999999998"),(41, "-9.999999998"),(42, "9.099999999"),(43, "-9.099999999"),(44, "9.900000000"),(45, "-9.900000000"),(46, "9.900000001"),(47, "-9.900000001"),(48, "9.000000000"),(49, "-9.000000000"),(50, "9.000000001"),(51, "-9.000000001"),(52, "9.000000009"),(53, "-9.000000009"),(54, "9.999999999"),(55, "-9.999999999"),(56, "9.999999998"),(57, "-9.999999998"),(58, "9.099999999"),(59, "-9.099999999"),
      (60, "9.900000000"),(61, "-9.900000000"),(62, "9.900000001"),(63, "-9.900000001"),(64, "8.000000000"),(65, "-8.000000000"),(66, "8.000000001"),(67, "-8.000000001"),(68, "8.000000009"),(69, "-8.000000009"),(70, "8.999999999"),(71, "-8.999999999"),(72, "8.999999998"),(73, "-8.999999998"),(74, "8.099999999"),(75, "-8.099999999"),(76, "8.900000000"),(77, "-8.900000000"),(78, "8.900000001"),(79, "-8.900000001"),
      (80, "0.000000000"),(81, "0.000000000"),(82, "0.000000001"),(83, "-0.000000001"),(84, "0.000000009"),(85, "-0.000000009"),(86, "0.999999999"),(87, "-0.999999999"),(88, "0.999999998"),(89, "-0.999999998"),(90, "0.099999999"),(91, "-0.099999999"),(92, "0.900000000"),(93, "-0.900000000"),(94, "0.900000001"),(95, "-0.900000001"),(96, "9.000000000"),(97, "-9.000000000"),(98, "9.000000001"),(99, "-9.000000001"),
      (100, "9.000000009"),(101, "-9.000000009"),(102, "9.999999999"),(103, "-9.999999999"),(104, "9.999999998"),(105, "-9.999999998"),(106, "9.099999999"),(107, "-9.099999999"),(108, "9.900000000"),(109, "-9.900000000"),(110, "9.900000001"),(111, "-9.900000001"),(112, "8.000000000"),(113, "-8.000000000"),(114, "8.000000001"),(115, "-8.000000001"),(116, "8.000000009"),(117, "-8.000000009"),(118, "8.999999999"),(119, "-8.999999999"),
      (120, "8.999999998"),(121, "-8.999999998"),(122, "8.099999999"),(123, "-8.099999999"),(124, "8.900000000"),(125, "-8.900000000"),(126, "8.900000001"),(127, "-8.900000001")
      ,(128, null);
    """

    sql "set enable_strict_cast=true;"
    qt_sql_0_strict 'select f1, cast(f2 as tinyint) from test_cast_to_tinyint_from_decimal64_10_9_0_nullable order by 1;'

    sql "set enable_strict_cast=false;"
    qt_sql_0_non_strict 'select f1, cast(f2 as tinyint) from test_cast_to_tinyint_from_decimal64_10_9_0_nullable order by 1;'

    sql "drop table if exists test_cast_to_tinyint_from_decimal64_10_9_0_not_nullable;"
    sql "create table test_cast_to_tinyint_from_decimal64_10_9_0_not_nullable(f1 int, f2 decimalv3(10, 9)) properties('replication_num'='1');"
    sql """insert into test_cast_to_tinyint_from_decimal64_10_9_0_not_nullable values (0, "0.000000000"),(1, "0.000000000"),(2, "0.000000001"),(3, "-0.000000001"),(4, "0.000000009"),(5, "-0.000000009"),(6, "0.999999999"),(7, "-0.999999999"),(8, "0.999999998"),(9, "-0.999999998"),(10, "0.099999999"),(11, "-0.099999999"),(12, "0.900000000"),(13, "-0.900000000"),(14, "0.900000001"),(15, "-0.900000001"),(16, "1.000000000"),(17, "-1.000000000"),(18, "1.000000001"),(19, "-1.000000001"),
      (20, "1.000000009"),(21, "-1.000000009"),(22, "1.999999999"),(23, "-1.999999999"),(24, "1.999999998"),(25, "-1.999999998"),(26, "1.099999999"),(27, "-1.099999999"),(28, "1.900000000"),(29, "-1.900000000"),(30, "1.900000001"),(31, "-1.900000001"),(32, "9.000000000"),(33, "-9.000000000"),(34, "9.000000001"),(35, "-9.000000001"),(36, "9.000000009"),(37, "-9.000000009"),(38, "9.999999999"),(39, "-9.999999999"),
      (40, "9.999999998"),(41, "-9.999999998"),(42, "9.099999999"),(43, "-9.099999999"),(44, "9.900000000"),(45, "-9.900000000"),(46, "9.900000001"),(47, "-9.900000001"),(48, "9.000000000"),(49, "-9.000000000"),(50, "9.000000001"),(51, "-9.000000001"),(52, "9.000000009"),(53, "-9.000000009"),(54, "9.999999999"),(55, "-9.999999999"),(56, "9.999999998"),(57, "-9.999999998"),(58, "9.099999999"),(59, "-9.099999999"),
      (60, "9.900000000"),(61, "-9.900000000"),(62, "9.900000001"),(63, "-9.900000001"),(64, "8.000000000"),(65, "-8.000000000"),(66, "8.000000001"),(67, "-8.000000001"),(68, "8.000000009"),(69, "-8.000000009"),(70, "8.999999999"),(71, "-8.999999999"),(72, "8.999999998"),(73, "-8.999999998"),(74, "8.099999999"),(75, "-8.099999999"),(76, "8.900000000"),(77, "-8.900000000"),(78, "8.900000001"),(79, "-8.900000001"),
      (80, "0.000000000"),(81, "0.000000000"),(82, "0.000000001"),(83, "-0.000000001"),(84, "0.000000009"),(85, "-0.000000009"),(86, "0.999999999"),(87, "-0.999999999"),(88, "0.999999998"),(89, "-0.999999998"),(90, "0.099999999"),(91, "-0.099999999"),(92, "0.900000000"),(93, "-0.900000000"),(94, "0.900000001"),(95, "-0.900000001"),(96, "9.000000000"),(97, "-9.000000000"),(98, "9.000000001"),(99, "-9.000000001"),
      (100, "9.000000009"),(101, "-9.000000009"),(102, "9.999999999"),(103, "-9.999999999"),(104, "9.999999998"),(105, "-9.999999998"),(106, "9.099999999"),(107, "-9.099999999"),(108, "9.900000000"),(109, "-9.900000000"),(110, "9.900000001"),(111, "-9.900000001"),(112, "8.000000000"),(113, "-8.000000000"),(114, "8.000000001"),(115, "-8.000000001"),(116, "8.000000009"),(117, "-8.000000009"),(118, "8.999999999"),(119, "-8.999999999"),
      (120, "8.999999998"),(121, "-8.999999998"),(122, "8.099999999"),(123, "-8.099999999"),(124, "8.900000000"),(125, "-8.900000000"),(126, "8.900000001"),(127, "-8.900000001");
    """

    sql "set enable_strict_cast=true;"
    qt_sql_0_strict 'select f1, cast(f2 as tinyint) from test_cast_to_tinyint_from_decimal64_10_9_0_not_nullable order by 1;'

    sql "set enable_strict_cast=false;"
    qt_sql_0_non_strict 'select f1, cast(f2 as tinyint) from test_cast_to_tinyint_from_decimal64_10_9_0_not_nullable order by 1;'

}